<?php
namespace app\admin\Controller;

use app\admin\model\Article_CategoryModel;
use app\admin\Model\ArticleModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Where;

Class ArticleController extends AdminBaseController{
    public function index () {
        //$title = config('test.title');
        //halt($title);
        //isset 用于检测某个变量是否设置
        //如果变量没有定义，返回false
        //如果变量被定义，返回true

        //empty()用于检测变量是否为空
        //如果变量的值不为空，返回false
        //如果变量的值为空，返回true
       extract(input());
        $where = new Where();
        if (!empty($start_time) && empty($end_time)) {
            $where['create_time'] = ['>=', strtotime($start_time)];
           // $where['create_time'] = ['>= time',$start_time];
        }
        if ( !empty($end_time) && empty($start_time)) {
            $where['create_time'] = ['<=', strtotime($end_time)];
            //$where['create_time'] = ['<= time', $end_time];
        }
        if ( !empty($start_time) &&!empty($end_time)) {
            $where['create_time'] = ['between', [strtotime($start_time),strtotime($end_time)]];
            //$where['create_time'] = ['between time', [$start_time,$end_time]];
        }
        $data = ArticleModel::with('Category')->where($where)->paginate(4,false,['query' => request()->param()]);
        $page = $data->render();
        return $this->fetch('',[
            'page' => $page,
           'data' => $data
        ]);
    }
    public function add () {
        $category = Article_CategoryModel::all();
        return $this->fetch('add',[
            'category' => $category
        ]);
    }
    public function addPost () {
        $data = input();
        //halt($data);
        //$data['create_time'] = time();

        $add = ArticleModel::create($data);
        if($add){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
    public function edit() {
        $id = input('art_id');
        $category = Article_CategoryModel::all();
        $data = ArticleModel::where('art_id',$id)->find();
        //halt($data);
        $this->assign('data',$data);
        $this->assign('category',$category);
        return $this->fetch();
    }
    public function editPost(){
        $data = input();
        $edit = ArticleModel::where('art_id',$data['art_id'])->update($data);
        if($edit){
            $this->success('修改成功','article/index');
        }else{
            $this->error('修改失败','article/index');
        }
    }
    public function check(){
        $id = input('art_id');
       // halt($id);
        $type = input('type');
        if ($type == 1){
            $is_recom = 2;
        }else {
            $is_recom = 1;
        }
        $edit = ArticleModel::where('art_id', $id)->update([
            'is_recom' => $is_recom
        ]);
        if($edit){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }

    }
    public function check2(){
        $id = input('art_id');
        // halt($id);
        $type = input('type');
        if ($type == 1){
            $status = 2;
        }else {
            $status = 1;
        }
        $edit = ArticleModel::where('art_id', $id)->update([
            'status' => $status
        ]);
        if($edit){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }

    }
    public function delete () {
        $id = input('art_id');
        $del = ArticleModel::where('art_id',$id)->delete();
        if($del){
            $this->success('删除成功','article/index');
        }else{
            $this->error('删除失败','article/index');
        }
    }
    public function delAll() {
        $ids = input('ids/a');
        $del = ArticleModel::where('art_id',$ids)->delete();
        if($del){
            $this->success('删除成功','article/index');
        }else{
            $this->error('删除失败','article/index');
        }
    }
    public function news () {
        $id = input('art_id');

        $data = ArticleModel::with('Category')->where('art_id',$id)->find();
        //halt($data);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function test () {
        //事务 1.开启 2.提交   3.回滚
        Db::startTrans();
        $add = Db::name('reason')->insert(['reason' => 1]);
        $del  = Db::name('reason')->delete(45);
        if ($add && $del){
            Db::commit();
        }else{
            Db::rollback();
        }
    }


}